package evanyark.weibo.view;

import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import evanyark.weibo.R;
import evanyark.weibo.util.OAuthConstant;

public class OAuthActivity extends Activity {
	private SharedPreferences sp;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.oauth);
		Uri uri=this.getIntent().getData();
		try {
			RequestToken requestToken= OAuthConstant.getInstance().getRequestToken();
			AccessToken accessToken=requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
			OAuthConstant.getInstance().setAccessToken(accessToken);
			TextView textView = (TextView) findViewById(R.id.TextView01);
			textView.setText("得到AccessToken的key和Secret,可以使用这两个参数进行授权登录了.\n Access token:\n"+accessToken.getToken()+"\n Access token secret:\n"+accessToken.getTokenSecret());
		
			sp = getSharedPreferences("SinaAccessToken", MODE_PRIVATE);  
			sp.edit().putString("token", accessToken.getToken()).putString("secret", accessToken.getTokenSecret()).commit();
			
			Toast.makeText(this, "token saved successfully,press to return..", Toast.LENGTH_LONG).show();
		} catch (WeiboException e) {
			e.printStackTrace();
		}

		
//		Intent i = new Intent(OAuthActivity.this, HomeTimeLineActivity.class);
//		startActivity(i);
	
		this.finish();
//		
	}
}
